//身份证校验及信息获取函数
import query from './query'
function getInfo(id, now_date) {
    let  info ={}
    if (id == null) {
        info ={status: "所检验值为空"}
        return  info ;
    }
     else {
        id = id.toString()
        id = id.replace(/(^\s*)|(\s*$)/g, "")  //去除空格
    }
    if (!(id.length == 18)) {
        info ={status:"长度不合法"}
        return info;
    }
    // 1 "验证通过!", 0 //校验不通过 // id为身份证号码
    var format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
    //号码规则校验
    if (!format.test(id)) {
        info ={status:"规则不合法"}
        return info;
    }
    //区位码校验
    //出生年月日校验  前正则限制起始年份为1900;
    let    year = id.substr(6, 4);//身份证年
    let   month = id.substr(10, 2);//身份证月
    let   date = id.substr(12, 2);//身份证日
    let    time = Date.parse(month + '-' + date + '-' + year);//身份证日期时间戳date
    let    now_time = Date.parse(new Date());//当前时间戳
    let    dates = (new Date(year, month, 0)).getDate();//身份证当月天数
    if (time > now_time || date > dates) {
        info ={status:"出生日期不合法"}
        return reText;
    }
    //获取性别
    let sex = id.substr(16, 1) % 2;
     sex = sex == 0 ? "女" : "男"
    //获取区划代码
    let code = id.substr(0, 6)+"000000"
    //获取出生日期
    let birth = year + "-" + month + "-" + date
    let birth1 = id.substr(6, 8)
    let birth2 = year + "年" +  parseInt(month) + "月" +  parseInt(date)+"日"
    //获取年龄

    let nowYear = now_date.substring(0, 4);
    let nowMonth = now_date.substring(4, 6);
    let nowDate = now_date.substring(6, 8);
    let age = nowYear - year;
    if (nowMonth < month || (nowMonth == month && nowDate < date)) {
        age--;
    }

    let address=query.getAreaCode(id.substr(0, 6))
    //获取身份证信息
    info = {
        status: "验证通过!",
        sex: sex,
        code: code,
        birth: birth,
        birth1: birth1,
        birth2: birth2,
        address: address,
        age: age
    }
    return info;
}

export default {
    getInfo
}
